/*
* enemy.cpp
*/
#include "enemy.h"
#include "consts.h"

Enemy::Enemy(Character * player, AnimatedSprite * pack, int type) : _player(player) {
  __copyPack(pack);
  __changeSequence(0);
  _type = type;
  _life = _maxLife = 2;
  _minrange = 80;
  _HUDLife = 0x00;
  __setBoundingBox(0, 0, 50, 91);
  __enableBoundingBox(1);
  __lockBoundingBox(1);

  float randSeed = (rand()%41);
  randSeed = randSeed/100 + 2.8;
  _stamina[1] = (int)(randSeed * FRAMERATE);
  randSeed = (rand()%41);
  randSeed = randSeed/100 + 0.8;
  _restTime[1] = (int)(randSeed * FRAMERATE);
  randSeed = (rand()%41);
  randSeed = randSeed/100 + 2.8;
  _shootFrequency[1] = (int)(randSeed * FRAMERATE);
  _view = VIEW_LEFT;
  
  // Boss
  if ( type == TYPE_BOSS ) {
    _maxLife = 20;
   _life = 20;
   _stamina[1] = (int) (3.4 * FRAMERATE);
   _restTime[1]= (int) (0.5 * FRAMERATE);
   _shootFrequency[1]= (int) (1.8 * FRAMERATE);
   _minrange = 160;
    
  }
    // IA stats
  _stamina[0] = _stamina[1];
  _restTime[0] = _restTime[1];
  _shootFrequency[0] = _shootFrequency[1];

}

/* __update()
*   @returns: 1 if alive, 0 if dead
*/
int Enemy::__update() {
  if ( _life < 1 ) return 0;
  __updateAnimation();

  int playerX = _player->__getX() + _player->__getSprite()->w / 2;
  // Changing Animation
  if ( __getX() > playerX ) {
     __changeSequence(ANIMATION_1);
     _view = VIEW_LEFT;
  }
  if ( __getX() + __getSprite()->w  < playerX ) {
     __changeSequence(ANIMATION_2);
     _view = VIEW_RIGHT;
  }
  // Stamina
  if ( _stamina[0] > 0) --_stamina[0];
  else {
    _restTime[0]--;
    if ( _restTime[0] < 1 ) { // Player ended resting

      _restTime[0] = _restTime[1];
      _stamina[0] = _stamina[1];
    }
  }
  // IA
  switch(_type) {
    default:
    case TYPE_BOSS:
    case TYPE_RANGED:
      _shootFrequency[0]--;
      if ( _shootFrequency[0] == 0 ) {
        _shootFrequency[0] = _shootFrequency[1];
        __shoot(_view);
      }
      if ( playerX < __getX() && __getX() - playerX > _minrange && _stamina[0] != 0)
        __moveX(-2);
      if ( playerX > __getX() && playerX - __getX() > _minrange && _stamina[0] != 0)
        __moveX(2);
    break;
  } 
  return 1;
}

/* __hit()
* @args:
*   - int damage : amount of damage to deal
*/
void Enemy::__hit(int damage) {
  _life -= damage;
  if ( _life < 0 )
  {
      _life = 0;
  }
  if ( _HUDLife) {
    float life = ((float)_life/(float)_maxLife) * 122;
    _HUDLife->__setStretchX(0, (int) life);
    _HUDLife->__update();
  }  
}

/*  __isAlive()
* @returns: 1 if alive, 0 if dead
*/
int Enemy::__isAlive() { return ( _life < 1 ) ? 0 : 1; }

/*  __shoot()
*
*/
void Enemy::__shoot(int side) {
  int speedx = 5;
  if ( side != VIEW_RIGHT ) speedx *= -1;
  eShot * p = new eShot(speedx, 0, 90, 1, 1);
  if ( side == VIEW_LEFT ) p->__setX(__getX());
  else p->__setX(__getX()+__getSprite()->w);
  p->__setY(__getY()+45);
  p->__setOwner(TYPE_ENEMY);
  entVec.push_back(p);
}

/* __setLife()
*
*/
void Enemy::__setLife(int val) {
  if ( val < 1 ) return;
  _life = _maxLife = val;
}
